home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / a_utils / perl / cterm.lha / cdefs.mk < prev    next >
Text File  |  1993-08-13  |  5KB  |  174 lines

  1. #! /local/bin/perl
  2.  
  3. do 'cdefs.types' ;
  4.  
  5. unlink('cdefs.c') ;
  6. unlink('cdefs.pl') ;
  7. open(STDIN,'cdefs.in') || die 'can\'t open cdefs.in' ; 
  8. open(C,'>cdefs.c')     || die 'can\'t write cdefs.c' ;
  9. open(P,'>cdefs.pl')    || die 'can\'t write cdefs.pl' ;
  10.  
  11. $curdef = '0' ;
  12. $sep = "\\034" ;
  13.  
  14. print C "#include <stdio.h>\n" ;
  15. print C "#include <curses.h>\n" ;
  16. print C "#include \"cursesXt.h\"\n" ;
  17. print C "#include \"defs.h\"\n" ;
  18. print C "\n" ;
  19. for ( sort keys %type )
  20.   { if ( $type{$_} eq 'int' )
  21.       { print C "static int $_ ;\n" ; }
  22.     elsif ( $type{$_} eq 'char' )
  23.       { print C "static char $_ ;\n" ; }
  24.     elsif ( $type{$_} eq 'str' )
  25.       { print C "static char $_\[BUFSIZE] ;\n" ; }
  26.     elsif ( $type{$_} eq 'win' )
  27.       { print C "static WINDOW *$_ ;\n" ; }
  28.   }
  29. print C "static int res ;\n" ;
  30. print C "\n" ;
  31.  
  32. while ( $_ = <STDIN> )
  33.   { chop ;
  34.     next if $_ =~ /^[ \t]*$/ || /^#/ ;
  35.     @ret = () ;
  36.     @arg = () ;
  37.     $varargs = 0 ;
  38.     $arg = '' ;
  39.     $body = '' ;
  40.     $form = '' ;
  41.     @_ = split ;
  42.     $func = shift @_ ;
  43.     push(@func,$func) ;
  44.     print C "void do_$func() {\n" ;
  45.     print C "  addlogs(LOGCURS1,\"$func\") ;\n" ;
  46.     print P "# $_\n" ;
  47.     print P "\$curfun{'$func'} = '$_' ;\n" ;
  48.     print P "sub $func {\n" ;
  49.     $form = "$curdef$sep" ;
  50.     for ( $i = 0 ; $i <= $#_ ; $i++ )
  51.       { $arg = $_[$i] ;
  52.     if ( $type{$arg} eq 'char' )
  53.           { $form .= "%c$sep" ;
  54.             print C "  $arg = nextChr() ;\n" ;
  55.       }
  56.     elsif ( $type{$arg} eq 'str' )
  57.           { $form .= "%s$sep" ;
  58.             print C "  strcpy($arg,nextStr()) ;\n" ;
  59.       }
  60.     elsif ( $type{$arg} eq 'int' )
  61.           { $form .= "%d$sep" ;
  62.             print C "  $arg = nextInt() ;\n" ;
  63.       }
  64.     elsif ( $type{$arg} eq 'win' )
  65.           { $form .= "%d$sep" ;
  66.             print C "  $arg = windows[nextInt()] ;\n" ;
  67.       }
  68.     elsif ( $type{$arg} eq 'var' )
  69.           { $arg =~ s/^&(.*)/$1/ ;
  70.         $varargs++ ;
  71.       }
  72.     elsif ( $arg eq 'RETURN' )
  73.       { @ret = @_[$i+1..$#_] ;
  74.         last ;
  75.       }
  76.     elsif ( $arg eq 'FLUSH' )
  77.       { last ; }
  78.     else
  79.       { print "unknown argument $arg in $_\n" ; }
  80.     push(@arg,$arg) ;
  81.       }
  82.  
  83.     if ( $#arg - $varargs < 0 )
  84.       { print P "  print(\"$form\") ;\n" ; }
  85.     elsif ( $#arg - $varargs == 0 )
  86.       { print P "  printf(\"$form\",\$_[0]) ;\n" ; }
  87.     else
  88.       { print P "  printf(\"$form\",\@_) ;\n" ; }
  89.  
  90.     if ( $#ret >= 0 )
  91.       { print P '  local($', join(',$',@ret), ") ;\n" ; }
  92.  
  93.     if ( $func eq 'initscr' )
  94.       { print C "  initscr() ;\n" ;
  95.     print C "  hasWin = 1 ;\n" ;
  96.     print C "  initWindows() ;\n" ;
  97.     print C "  saveWindow(stdscr) ;\n" ;
  98.     print C "  saveWindow(curscr) ;\n" ;
  99.     print C "  initCursesXt() ;\n" ;
  100.     $body .= "  \$stdscr = 0 ;\n" ;
  101.     $body .= "  \$curscr = 1 ;\n" ;
  102.     $body .= "  \$LINES = &getLINES ;\n" ;
  103.     $body .= "  \$COLS = &getCOLS ;\n" ;
  104.       }
  105.     elsif ( $func eq 'endwin' )
  106.       { print C "  endwin() ;\n" ;
  107.     print C "  hasWin = 0 ;\n" ;
  108.         $body .= "  &curFlush ;\n" ;
  109.       }
  110.     elsif ( $func eq 'delwin' )
  111.       { print C "  win = windows[$arg] ;\n" ;
  112.         print C "  unsaveWindow($arg) ;\n" ;
  113.         print C "  delwin(win) ;\n" ;
  114.       }
  115.     elsif ( $arg eq 'RETURN' )
  116.       { $body .= "  &curFlush ;\n" ;
  117.     @res = grep(/^res(int|str|win)$/,@ret) ;
  118.         if ( $#res > 0 )
  119.       { print "$_ : you may specify res... only once\n" ; }
  120.         elsif ( $#res == 0 )
  121.       { if ( $type{$res[0]} eq 'str' )
  122.           { print C "  strcpy($res[0],$func(". join(',',@arg) . ")) ;\n" ; }
  123.         else
  124.           { print C "  $res[0] = $func(". join(',',@arg) . ") ;\n" ; }
  125.       }
  126.     else
  127.       { print C "  $func(". join(',',@arg) . ") ;\n" ; }
  128.         for $ret ( @ret )
  129.       { $body .= "  \$$ret = <STDIN> ; chop(\$$ret) ;\n" ;
  130.         if ( $type{$ret} eq 'str' )
  131.           { print C "  fprintf(OUT,\"%s\\n\",$ret) ;\n" ;
  132.             print C "  addlogss(LOGCURS1," ;
  133.         print C "\"do_$func prints OUT\",$ret) ;\n" ;
  134.           }
  135.         elsif ( $type{$ret} eq 'win' )
  136.           { print C "  res = saveWindow($ret) ;\n" ;
  137.             print C "  fprintf(OUT,\"%d\\n\",res) ;\n" ;
  138.             print C "  addlogsn(LOGCURS1," ;
  139.         print C "\"do_$func prints OUT\",res) ;\n" ;
  140.           }
  141.         elsif ( $type{$ret} eq 'int' )
  142.           { print C "  fprintf(OUT,\"%d\\n\",$ret) ;\n" ;
  143.             print C "  addlogsn(LOGCURS1," ;
  144.         print C "\"do_$func prints OUT\",$ret) ;\n" ;
  145.           }
  146.         else
  147.           { print "I have no type for $ret\n" ; }
  148.       }
  149.     print C "  fflush(OUT) ;\n" ;
  150.         $body .= '  return $'. join(', $',@ret) . "  ;\n" ;
  151.       }
  152.     elsif ( $arg eq 'FLUSH' )
  153.       { $body .= "  &curFlush ;\n" ;
  154.         print C "  $func(". join(',',@arg) . ") ;\n" ;
  155.       }
  156.     else
  157.       { print C "  $func(". join(',',@arg) . ") ;\n" ; }
  158.  
  159.     print P $body, "}\n" ;
  160.     print C "}\n" ;
  161.     $curdef++ ;
  162.   }
  163.  
  164. print P "\n1 ;\n" ;
  165.  
  166. print C "void (*(funtab[$curdef]))() ;\n" ;
  167. print C "void init_funtab() {\n" ;
  168. $i = 0 ;
  169. for $func ( @func )
  170.   { print C "  funtab[$i] = do_$func ;\n" ;
  171.     $i++ ;
  172.   }
  173. print C "}\n" ;
  174.